package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.TaskDao;
import com.eric.husky.bean.Task;
import java.util.List;
import java.util.Map;

@Service
public class TaskService {
    @Autowired
	private TaskDao taskDao;

	public Page<Task> list(Task task, Pageable page) {
		Example<Task> example = Example.of(task);
		return taskDao.findAll(example, page);
	}

	public Task get(Integer taskid) {
		return taskDao.findById(taskid).get();
	}

    public List<Task> findList() {
		return taskDao.findAll();
	}

	@Transactional
	public Task save(Task task) {
	    if(task.getTaskid()!=null){
	        Task taskOld = taskDao.findById(task.getTaskid()).orElse(null);
	        if(taskOld!=null){
	            UpdateUtil.copyNullProperties(task,taskOld);
                return taskDao.save(taskOld);
	        }
	    }
	    return taskDao.save(task);
	}

	@Transactional
	public void delete(Integer taskid) {
		taskDao.deleteById(taskid);
	}

	public Page<Map<String , Object>> getTaskPage(Pageable pageable, String keywords){
		return taskDao.getTaskPage(pageable,keywords);
	}

}